package com.neuronbit.businessflow.intellij.psi;

import com.intellij.refactoring.changeSignature.ParameterInfo;
import com.neuronbit.businessflow.intellij.BpmFileType;
import com.neuronbit.businessflow.intellij.lang.PsiBpmFile;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileFactory;
import org.antlr.intellij.adaptor.psi.ANTLRPsiNode;

import java.util.List;

/**
 * The type Simple element factory.
 */
public class SimpleElementFactory {
//
//    public static SimpleProperty createProperty(Project project, String name) {
//        final SimpleFile file = createFile(project, name);
//        return (SimpleProperty) file.getFirstChild();
//    }

    /**
     * Create file psi bpm file.
     *
     * @param project the project
     * @param text    the text
     * @return the psi bpm file
     */
    public static PsiBpmFile createFile(Project project, String text) {
        String name = "dummy.bpm";
        return (PsiBpmFile) PsiFileFactory.getInstance(project).createFileFromText(name, BpmFileType.INSTANCE, text);
    }

    /**
     * Create string antlr psi node.
     *
     * @param project the project
     * @param text    the text
     * @return the antlr psi node
     */
//
    public static ANTLRPsiNode createString(Project project, String text) {
        final PsiBpmFile file = createFile(project, text);
        return (ANTLRPsiNode) file.getFirstChild();
    }

    /**
     * Create call psi element.
     *
     * @param project the project
     * @param newText the new text
     * @return the psi element
     */
    public static PsiElement createCall(Project project, String newText) {
        String text = "<mxGraphModel>\n" +
                "    <root>\n" +
                "        <mxCell>\n" +
                "            <com.neuronbit.businessflow.intellij.model.AutoTask>\n" +
                newText +
                "            </com.neuronbit.businessflow.intellij.model.AutoTask>\n" +
                "        </mxCell>\n" +
                "    </root>\n" +
                "</mxGraphModel>";
        final PsiBpmFile file = createFile(project, text);
        PsiElement[] call = file.getCall();
        return call[0];
    }

    /**
     * Create attribute psi element.
     *
     * @param project the project
     * @param name    the name
     * @param value   the value
     * @return the psi element
     */
    public static PsiElement createAttribute(Project project,String name, String value) {
        String text = "<mxGraphModel>\n" +
                "    <root>\n" +
                "        <mxCell "+name+"=\""+ value +"\" />"+
                "    </root>\n" +
                "</mxGraphModel>";
        final PsiBpmFile file = createFile(project, text);
        PsiElement[] call = file.getAttribute();
        return call[0];
    }

    /**
     * Create param array psi element.
     *
     * @param project       the project
     * @param method        the method
     * @param newParameters the new parameters
     * @return the psi element
     */
    public static PsiElement createParamArray(Project project, List<PsiElement> method, ParameterInfo[] newParameters) {
//        <add as="arg" value="flow.name" type="java.lang.String"/>
//        Arrays.stream(newParameters).map()
//        "<com.neuronbit.businessflow.intellij.model.Call><Array>"+adf+"</Array></com.neuronbit.businessflow.intellij.model.Call>"
        return null;
    }
//
//    public static PsiElement createCRLF(Project project) {
//        final SimpleFile file = createFile(project, "\n");
//        return file.getFirstChild();
//    }

}
